import datetime
import logging
import threading

runningEvent = threading.Event()
#import colorlog
logging.basicConfig(level=logging.DEBUG,
    format='%(asctime)s %(message)s',
    datefmt='%H:%M:%S',
    filename='myapp.log',
    filemode='w')

LOGGER = logging.getLogger("ctgust_bms")

console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(name)-12s: %(levelname)-5s %(message)s')
formatter.datefmt = '%H:%M:%S'
console.setFormatter(formatter)
LOGGER.addHandler(console)


class LoggingFilter(logging.Filter):
    def __init__(self, second=10,name=""):
        super().__init__(name)
        self.deltaSecond = second
        self.lasttime = datetime.datetime.now()    
    def filter(self, record):
        curtime = datetime.datetime.now()
        if(self.lasttime+datetime.timedelta(seconds = self.deltaSecond)<curtime):
            self.lasttime = curtime
            return True
        else:
            return False
def getDataLogger(logname,filename,interval=10):
    '''interval:ms'''
    dataLogger = logging.getLogger(logname)#'datalogger')
    dataLogger.setLevel(logging.INFO)
##rf_handler = logging.handlers.TimedRotatingFileHandler('all.log', when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0, 0))
##rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
    f_handler = logging.FileHandler(filename)#'monitordata-200ms.log')
    f_handler.setLevel(logging.INFO)
    f_handler.setFormatter(logging.Formatter("%(asctime)s  %(message)s"))
    dataLogger.addHandler(f_handler)
    if( interval>0):
        dataLogger.addFilter(LoggingFilter(second=1.0*interval/1000))
    return dataLogger
    '''
    dataLogger1s = logging.getLogger('datalogger1s')
    dataLogger1s.setLevel(logging.INFO)
    f_handler = logging.FileHandler('monitordata-1s.log')
    f_handler.setLevel(logging.INFO)
    f_handler.setFormatter(logging.Formatter("%(asctime)s  %(message)s"))
    dataLogger1s.addHandler(f_handler)
    dataLogger1s.addFilter(LoggingFilter(second=1))

    dataLogger10s = logging.getLogger('datalogger10s')
    dataLogger10s.setLevel(logging.INFO)
    f_handler = logging.FileHandler('monitordata-10s.log')
    f_handler.setLevel(logging.INFO)
    f_handler.setFormatter(logging.Formatter("%(asctime)s  %(message)s"))
    dataLogger10s.addHandler(f_handler)
    dataLogger10s.addFilter(LoggingFilter(second=10))
'''


VERSION = '1.1.2'